package com.bhs.sansong.ui.dialog;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bhs.sansong.R;
import com.bhs.sansong.net.Config;
import com.bhs.sansong.ui.other.WebViewActivity;
import com.bhs.sansong.utils.SharedPreferencesUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.enums.PopupPosition;
import com.lxj.xpopup.util.XPopupUtils;

/**
 * 隐私协议弹窗
 */
public class PrivacyAgreementDialog extends CenterPopupView implements View.OnClickListener {

    private TextView btnAgree;
    private TextView tvDisagree;
    private Activity activity;
    private LinearLayout mLlAgreement;
    private ImageView mIvAgreement;
    private TextView mTvAgreement;
    private boolean isChecked;

    public PrivacyAgreementDialog(@NonNull Activity context) {
        super(context);
        this.activity = context;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_privacy_agreement;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        btnAgree = findViewById(R.id.btn_agree);
        tvDisagree = findViewById(R.id.tv_disagree);
        mLlAgreement = findViewById(R.id.ll_agreement);
        mIvAgreement = findViewById(R.id.iv_agreement);
        mTvAgreement = findViewById(R.id.tv_agreement);
        btnAgree.setOnClickListener(this);
        tvDisagree.setOnClickListener(this);
        mLlAgreement.setOnClickListener(this);
        String str = "勾选代表您同意《用户协议》、《隐私政策》";
        SpannableStringBuilder spannableBuilder = new SpannableStringBuilder(str);
        // 在设置点击事件、同时设置字体颜色
        ClickableSpan clickableSpanOne = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                activity.startActivity(new Intent(activity, WebViewActivity.class)
                        .putExtra("url", Config.getUserProtocolUrl())
                        .putExtra("title", "用户协议"));
            }

            @Override
            public void updateDrawState(TextPaint paint) {
                paint.setColor(Color.parseColor("#485EF4"));
                // 设置下划线 true显示、false不显示
                paint.setUnderlineText(false);
            }
        };
        ClickableSpan clickableSpanThree = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                activity.startActivity(new Intent(activity, WebViewActivity.class)
                        .putExtra("url", Config.getPrivacyPolicyUrl())
                        .putExtra("title", "隐私协议"));
            }

            @Override
            public void updateDrawState(TextPaint paint) {
                paint.setColor(Color.parseColor("#485EF4"));
                // 设置下划线 true显示、false不显示
                paint.setUnderlineText(false);
            }
        };
        spannableBuilder.setSpan(clickableSpanOne, 7, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableBuilder.setSpan(clickableSpanThree, 14, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTvAgreement.setMovementMethod(LinkMovementMethod.getInstance());
        mTvAgreement.setText(spannableBuilder);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_agree) {
            if (!isChecked) {
                new XPopup.Builder(getContext())
                        .hasShadowBg(false) // 去掉半透明背景
                        .popupPosition(PopupPosition.Top)
                        .atView(mIvAgreement)
                        .offsetY(XPopupUtils.dp2px(getContext(), 6))
                        .asCustom(new CustomBubbleAttachPopup(getContext())
                                .setBubbleBgColor(Color.parseColor("#7F000000"))  //气泡背景
                                .setArrowWidth(XPopupUtils.dp2px(getContext(), 5))
                                .setArrowHeight(XPopupUtils.dp2px(getContext(), 6))
                                .setArrowRadius(XPopupUtils.dp2px(getContext(), 3))
                        )
                        .show();
            } else {
                dismiss();
                listener.onClick(v);
            }
        } else if (id == R.id.ll_agreement) {
            if (isChecked) {
                mIvAgreement.setImageResource(R.mipmap.ic_unchecked);
            } else {
                mIvAgreement.setImageResource(R.mipmap.ic_checked);
            }
            isChecked = !isChecked;
            SharedPreferencesUtils.saveBoolean(getContext(), "privacy", isChecked);
        } else {
            activity.finish();
        }
    }

    private MyClickListener listener;

    public void setMyClickListener(@Nullable MyClickListener l) {
        this.listener = l;
    }
}


